package com.lemeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.lemeng.dao.AlbumMapper;
import com.lemeng.dao.ParaMapper;
import com.lemeng.dao.SpecMapper;
import com.lemeng.entity.ImageItem;
import com.lemeng.entity.PageResult;
import com.lemeng.pojo.Album;
import com.lemeng.service.AlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = AlbumService.class)
public class AlbumServiceImpl implements AlbumService {

    @Autowired
    AlbumMapper albumMapper;

    @Override
    public List<Album> findAll() {
        return albumMapper.selectAll();
    }

    @Override
    public PageResult<Album> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        PageInfo<Album> albumPageInfo = new PageInfo<>(albumMapper.selectAll());
//        Page<Album> albums = (Page<Album>)albumMapper.selectAll();
        return new PageResult(albumPageInfo.getTotal(),albumPageInfo.getList());
    }

    @Override
    public List<Album> findList(Map<String, Object> serchMap) {
        Example example = new Example(Album.class);
        Example.Criteria criteria = example.createCriteria();
        if (serchMap != null) {
            if (serchMap.get("title")!=null && StringUtil.isNotEmpty(serchMap.get("title").toString())){
                criteria.andLike("title","%"+serchMap.get("title").toString()+"%");
            }
        }
        return albumMapper.selectByExample(example);
    }

    @Override
    public PageResult<Album> findPage(Integer page, Integer size, Map<String, Object> serchMap) {
        PageHelper.startPage(page,size);
        PageInfo<Album> albumPageInfo = new PageInfo<>(findList(serchMap));
        return new PageResult(albumPageInfo.getTotal(),albumPageInfo.getList());
    }

    @Override
    public Album findById(Integer id) {
        return albumMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Album album) {
        album.setImageItems("[]");
        albumMapper.insert(album);
    }

    @Override
    public void update(Album album) {
        albumMapper.updateByPrimaryKeySelective(album);
    }

    @Autowired
    SpecMapper specMapper;

    @Autowired
    ParaMapper paraMapper;

    @Override
    @Transactional
    public void delete(Integer id) {
        albumMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void addImgItem(Album album) {
        // 传入参数：id  imageItems
        //先找出数据库这个id的imageItems的值
        //然后把前台传的拼接进去即可
        Album album1 = albumMapper.selectByPrimaryKey(album.getId());
        String oldStr = album1.getImageItems();
        String newStr = album.getImageItems();
        //伪代码，需求思路，编程思维：思路一
        //先把old转成list，再把new转成list，然后合并两个list，再把合并之后的list转成支持符传，set到对象里，然后再更新到数据库
//        List<ImageItem> oldList = JSONObject.parseArray(oldStr, ImageItem.class);
//        List<ImageItem> newList = JSONObject.parseArray(newStr, ImageItem.class);
//        oldList.addAll(newList);
//        String allStr = JSONObject.toJSONString(oldList);
//        album1.setImageItems(allStr);
//        albumMapper.updateByPrimaryKeySelective(album1);


        if(oldStr.equals("[]"))oldStr = newStr;
        else oldStr = oldStr.substring(0,oldStr.length()-1)+","+newStr.substring(1);
        album1.setImageItems(oldStr);
        albumMapper.updateByPrimaryKeySelective(album1);
    }

    @Override
    public void removeImageItems(Long id,Long uid) {
        //先查出相册对象
        //先把相册图片字段的字符串转成list
        //遍历list，匹配uid，如果匹配，就移除这个对象
        //最后把移除之后的list，再转成字符串，set到相册对象里
        //更新到数据库
        Album album1 = albumMapper.selectByPrimaryKey(id);
        List<ImageItem> imageItemList = JSONObject.parseArray(album1.getImageItems(), ImageItem.class);
        for (int i = 0; i < imageItemList.size(); i++) {
            if (imageItemList.get(i).getUid().equals(uid)){
                imageItemList.remove(i);
                break;
            }
        }
        album1.setImageItems(JSONObject.toJSONString(imageItemList));
        albumMapper.updateByPrimaryKeySelective(album1);
    }

    @Override
    @Transactional
    public void moveAlbum(Long newId, Long oldId, Long uid) {
        //移除老相册的
        Album album1 = albumMapper.selectByPrimaryKey(oldId);
        List<ImageItem> imageItemList = JSONObject.parseArray(album1.getImageItems(), ImageItem.class);
        ImageItem imageItem = new ImageItem();
        for (int i = 0; i < imageItemList.size(); i++) {
            if (imageItemList.get(i).getUid().equals(uid)){
                imageItem = imageItemList.get(i);
                imageItemList.remove(i);
                break;
            }
        }
        album1.setImageItems(JSONObject.toJSONString(imageItemList));

        //添加到新相册里
        Album albumNew = albumMapper.selectByPrimaryKey(newId);
        List<ImageItem> imageItemListNew = JSONObject.parseArray(albumNew.getImageItems(), ImageItem.class);
        imageItemListNew.add(imageItem);
        albumNew.setImageItems(JSONObject.toJSONString(imageItemListNew));

        //更新到数据库
        albumMapper.updateByPrimaryKeySelective(album1);
        albumMapper.updateByPrimaryKeySelective(albumNew);
    }


}
